package com.lyhengtongwl.zqsnews.ui.activity;

import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.flyco.tablayout.SlidingTabLayoutNew;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.adapter.ForwardAdapter;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.ui.fragment.prentice.PrenticeFragment;
import com.lyhengtongwl.zqsnews.widget.CustomScrollViewPager;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Response;

public class NewsDDListActivityNew extends BaseActivity {
    private String typeName;
    @BindView(R.id.slidingPaneLayout)
    SlidingTabLayoutNew stl;
    @BindView(R.id.vp)
    CustomScrollViewPager vp;
    @BindView(R.id.tv_title1)
    TextView title1;
    @BindView(R.id.tv_title2)
    TextView title2;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ll_noRecord)
    LinearLayout ll_noRecord;
    private String type;

    @BindView(R.id.tv_count1)
    TextView tv_count1;

    @BindView(R.id.tv_count2)
    TextView tv_count2;
    @BindView(R.id.iv_title_right)
    ImageView iv_title_right;

    private int count;
    private int prenticeCount;
    private int withDraw;

    @Override
    public void setListen() {

    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_ddlist_new;
    }

    @Override
    public void initView() {
        stl.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (position == 1) {
                    if (prenticeCount == 0) {
                        llTitle.setVisibility(View.GONE);
                    } else {
                        llTitle.setVisibility(View.VISIBLE);
                    }
                } else {

                    llTitle.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @Override
    public void initData() {
        List<String> mTitles = new ArrayList<>();
        type = getIntent().getStringExtra("type") + "";
        switch (type) {
            case "0":
                tvName.setText("我的徒弟");
                typeName = "我的徒弟";
                mTitles.add("全部徒弟");
                mTitles.add("有效徒弟");
                mTitles.add("尚未提现");
                title1.setText("收徒总人数(人)");
                title2.setText("有效徒弟/未提现(人)");
                break;
            case "1":
                tvName.setText("我的徒孙");
                typeName = "我的徒孙";
                mTitles.add("全部徒孙");
                mTitles.add("有效徒孙");
                mTitles.add("尚未提现");
                title1.setText("徒孙总人数(人)");
                title2.setText("有效徒孙/未提现(人)");
                break;
        }
        iv_title_right.setVisibility(View.GONE);
        new TitleBuilder(this).setTitleText(typeName)
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
        List<Fragment> mFragments = new ArrayList<>();
        for (int i = 0; i < mTitles.size(); i++) {
            mFragments.add(new PrenticeFragment(i + "", type));
        }

        getAllPrentice();


        ForwardAdapter forwardAdapter = new ForwardAdapter(getSupportFragmentManager(), mFragments, mTitles);
        //设置ViewPager与适配器关联
        vp.setAdapter(forwardAdapter);
        vp.setOffscreenPageLimit(mTitles.size() - 1);
        //设置Tab与ViewPager关联
        stl.setViewPager(vp);
        stl.setCurrentTab(0);
    }

    //获取数据
    private void getAllPrentice() {
        Task.getApiService().getAllPrentice().enqueue(new MyCallback<NewsBaseEntity>(NewsDDListActivityNew.this) {
            @SuppressLint("SetTextI18n")
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                if ("1".equals(response.body().getCode())) {
                    String jsonStr = JSONObject.toJSONString(response.body().getData());
                    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
                    if ("0".equals(type)) {
                        count = jsonObject.getInteger("prenticeCount");
                        prenticeCount = jsonObject.getInteger("validPrenticeCount");
                        withDraw = jsonObject.getInteger("notPrenticeCount");
                        tv_count1.setText("" + count);
                        tv_count2.setText("" + prenticeCount + "/" + withDraw);
                    } else {
                        count = jsonObject.getInteger("discipleCount");
                        prenticeCount = jsonObject.getInteger("validDiscipleCount");
                        withDraw = jsonObject.getInteger("notDiscipleCount");
                        tv_count1.setText("" + count);
                        tv_count2.setText("" + prenticeCount + "/" + withDraw);
                    }
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }
}
